package com.jack.online.study.controller.edu.admin;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.jack.online.study.common.base.result.R;
import com.jack.online.study.model.edu.form.CourseInfoForm;
import com.jack.online.study.model.edu.vo.CoursePublishVo;
import com.jack.online.study.model.edu.vo.CourseQueryVo;
import com.jack.online.study.model.edu.vo.CourseVo;
import com.jack.online.study.service.edu.CourseService;
import com.jack.online.study.service.edu.VideoService;
import jakarta.annotation.Resource;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 * 课程 前端控制器
 * </p>
 *
 * @author jack
 * @since 2024-04-17
 */
@CrossOrigin
@RestController
@RequestMapping("/admin/edu/course")
public class CourseController {

    @Resource
    private CourseService courseService;

    @Resource
    private VideoService videoService;

    /**
     * 新增课程
     *
     * @param courseInfoForm 课程基本信息
     */
    @PostMapping("save-course-info")
    public R saveCourseInfo(@RequestBody CourseInfoForm courseInfoForm) {
        String courseId = courseService.saveCourseInfo(courseInfoForm);
        return R.ok().data("courseId", courseId).message("保存成功");
    }

    /**
     * 根据ID查询课程
     *
     * @param id 课程ID
     */
    @GetMapping("course-info/{id}")
    public R getById(@PathVariable String id) {

        CourseInfoForm courseInfoForm = courseService.getCourseInfoById(id);
        if (courseInfoForm != null) {
            return R.ok().data("item", courseInfoForm);
        } else {
            return R.error().message("数据不存在");
        }
    }

    /**
     * 更新课程
     *
     * @param courseInfoForm 课程基本信息
     */
    @PutMapping("update-course-info")
    public R updateCourseInfoById(@RequestBody CourseInfoForm courseInfoForm) {

        courseService.updateCourseInfoById(courseInfoForm);
        return R.ok().message("修改成功");
    }

    /**
     * 分页课程列表
     *
     * @param page          当前页码
     * @param limit         每页记录数
     * @param courseQueryVo 查询对象
     */
    @GetMapping("list/{page}/{limit}")
    public R index(@PathVariable Long page,
                   @PathVariable Long limit,
                   CourseQueryVo courseQueryVo) {
        IPage<CourseVo> pageModel = courseService.selectPage(page, limit, courseQueryVo);
        List<CourseVo> records = pageModel.getRecords();
        long total = pageModel.getTotal();
        return R.ok().data("total", total).data("rows", records);
    }

    /**
     * 根据ID删除课程
     * @param id 课程ID
     */
    @DeleteMapping("remove/{id}")
    public R removeById(@PathVariable String id) {

        // 删除视频：VOD
        //在此处调用vod中的删除视频文件的接口
        videoService.removeMediaVideoByCourseId(id);
        //删除封面：OSS 返回值是布尔不管

        courseService.removeCoverById(id);
        //删除课程
        boolean result = courseService.removeCourseById(id);
        if (result) {
            return R.ok().message("删除成功");
        } else {
            return R.error().message("数据不存在");
        }
    }

    /**
     * 根据ID获取课程发布信息
     * @param id 课程ID
     */
    @GetMapping("course-publish/{id}")
    public R getCoursePublishVoById(@PathVariable String id) {
        CoursePublishVo coursePublishVo = courseService.getCoursePublishVoById(id);
        if (coursePublishVo != null) {
            return R.ok().data("item", coursePublishVo);
        } else {
            return R.error().message("数据不存在");
        }
    }

    /**
     * 根据id发布课程
     * @param id 课程ID
     */
    @PutMapping("publish-course/{id}")
    public R publishCourseById(@PathVariable String id) {
        boolean result = courseService.publishCourseById(id);
        if (result) {
            return R.ok().message("发布成功");
        } else {
            return R.error().message("数据不存在");
        }
    }
}
